<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2
 * @license    GNU/GPL
	*/

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the HelloWorld Component
 *
 * @package    HelloWorld
 */

class NewMediaViewProgramme extends JView
{
	function display($tpl = null)
	{
		global $mainframe;
		
		$model =& $this->getModel();

		$user		=& JFactory::getUser();
		$params		=& $mainframe->getParams('com_content');

		$lists = $this->_buildEditLists();

		$this->assignRef('lists',	$lists);
		$this->assignRef('params' , $params);
		$this->assignRef('user'   , $user);

		parent::display($tpl);
	}

	function _buildEditLists()
	{
		// Get the article and database connector from the model
		$article = & $this->get('Article');
		$db 	 = & JFactory::getDBO();
		$sectionid = 1;

		$javascript = "onchange=\"llenarMediosProgramas(document.adminForm.catid.value,document. adminForm.provinciaid.selectedIndex,document. adminForm.medid.selectedIndex);changeDynaList( 'catid', sectioncategories, document.adminForm.sectionid.options[document.adminForm.sectionid.selectedIndex].value, 0, 0);changeDynaList( 'medid', categorymedios, document.adminForm.catid.options[document.adminForm.catid.selectedIndex].value, 0, 0);changeDynaList( 'progid', medioprogramas, document.adminForm.medid.options[document.adminForm.medid.selectedIndex].value, 0, 0);\"";
		$javascriptCategory = "onchange=\"llenarMedios(document.adminForm.catid.options[document.adminForm.catid.selectedIndex].value,document.adminForm.provinciaid.options[document.adminForm.provinciaid.selectedIndex].value);\"";
		$javascriptProvincia = "onchange=\"llenarMedios(document.adminForm.catid.options[document.adminForm.catid.selectedIndex].value,document.adminForm.provinciaid.options[document.adminForm.provinciaid.selectedIndex].value);\"";
		$javascriptMedio = ""; //"onchange=\"llenarProgramas(document.adminForm.medid.options[document.adminForm.medid.selectedIndex].value);\"";
		
		$query = 'SELECT s.id, s.title' .
				' FROM #__sections AS s' .
				' WHERE s.published = 1' .
				' ORDER BY s.ordering';
		$db->setQuery($query);

		$sections[] = JHTML::_('select.option', '-1', '- '.JText::_('Select Section').' -', 'id', 'title');
		$sections[] = JHTML::_('select.option', '0', JText::_('Uncategorized'), 'id', 'title');
		$sections = array_merge($sections, $db->loadObjectList());
		$lists['sectionid'] = JHTML::_('select.genericlist',  $sections, 'sectionid', 'class="inputbox" size="1" '.$javascript, 'id', 'title', intval($sectionid));

		foreach ($sections as $section)
		{
			$section_list[] = (int) $section->id;
			// get the type name - which is a special category
			if ($sectionid) {
				if ($section->id == $sectionid) {
					$contentSection = $section->title;
				}
			} else {
				if ($section->id == $sectionid) {
					$contentSection = $section->title;
				}
			}
		}

		$sectioncategories = array ();
		$sectioncategories[-1] = array ();
		$sectioncategories[-1][] = JHTML::_('select.option', '-1','- '.JText::_( 'Select Category' ).' -', 'id', 'title');
		$section_list = implode('\', \'', $section_list);

		$query = 'SELECT id, title, section' .
				' FROM #__categories' .
				' WHERE section IN ( \''.$section_list.'\' )' .
				' ORDER BY ordering';
		$db->setQuery($query);
		$cat_list = $db->loadObjectList();

		// Uncategorized category mapped to uncategorized section
		$uncat = new stdClass();
		$uncat->id = 0;
		$uncat->title = JText::_('Uncategorized');
		$uncat->section = 0;
		$cat_list[] = $uncat;
		foreach ($sections as $section)
		{
			$sectioncategories[$section->id] = array ();
			$rows2 = array ();
			foreach ($cat_list as $cat)
			{
				if ($cat->section == $section->id) {
					$rows2[] = $cat;
				}
			}
			foreach ($rows2 as $row2) {
				$sectioncategories[$section->id][] = JHTML::_('select.option', $row2->id, $row2->title, 'id', 'title');
			}
		}

		$categorymedios = array ();
		$categorymedios[-1] = array ();
		$categorymedios[-1][] = JHTML::_('select.option', '-1','- '. JText::_( 'Select Media' ). ' -', 'id', 'title');

		$categories = array();
		$categories[] = JHTML::_('select.option', '-1','- '.JText::_( 'Seleccione un tipo de medio' ).' -', 'id', 'title');
		foreach ($cat_list as $cat) {
			if($cat->section == $sectionid) {
				$categories[] = $cat;
			}
			$categorymedios[$cat->id] = array();
			$categorymedios[$cat->id][] = JHTML::_('select.option', '-1','- '.JText::_( 'Select Media' ).' -', 'id', 'title');
		}
 
		$lists['sectioncategories'] = $sectioncategories;
		$lists['catid'] = JHTML::_('select.genericlist',  $categories, 'catid', ' class="inputbox" size="1" '.$javascriptCategory , 'id', 'title', -1);

		$query = 'SELECT s.id, s.title, s.catid' .
				' FROM #__medio AS s ' .
				' INNER JOIN #__provincias AS p ON s.provid = p.id ' .
				($article->catid != null  ? ' WHERE catid = '. $article->catid : '' ) .
				($article->provinciaid != null  ? ' AND provid = '. $article->provinciaid : '' ).
				' ORDER BY p.ordering ';
				
		$db->setQuery($query);

		$med_list = $db->loadObjectList();
		$med_list = ($med_list != null) ? $med_list : array();
		
		foreach ($med_list as $medio) {
			$categorymedios[$medio->catid][] = JHTML::_('select.option', $medio->id, $medio->title, 'id', 'title');
		}
		
		foreach ($cat_list as $cat) {
			$categorymedios[$cat->id][] = JHTML::_('select.option', '0','-- Otro --', 'id', 'title');
		}

		$lists['categorymedios'] = $categorymedios;
		$medios[] = JHTML::_('select.option', '-1', '- '.JText::_('Seleccionar un medio').' -', 'id', 'title');

		$lists['medid'] = JHTML::_('select.genericlist',  $medios, 'medid', 'class="inputbox" size="1" '. $javascriptMedio, 'id', 'title', -1);
		
		$query = 'SELECT p.id, p.title' .
			' FROM #__provincias AS p'.
			' ORDER BY ordering';
		$db->setQuery($query);
		$provincias[] = JHTML::_('select.option', '-1', '- '.JText::_('Select scope').' -', 'id', 'title');
		$provincias = array_merge($provincias, $db->loadObjectList());
		
		$lists['provinciaid'] = JHTML::_('select.genericlist',  $provincias, 'provinciaid', 'class="inputbox" size="1" '. $javascriptProvincia, 'id', 'title', intval($article->provinciaid));


		return $lists;

	}
}
